package com.videoplat.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.DecimalFormat;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

/**
 * 
 * @author wangyan
 *
 */
public class DecimalUtils {
    final static Logger logger = (Logger) LoggerFactory.getLogger(DecimalUtils.class);

    /** #,##0.00 */
    public static final String PATTERN = "#,##0.00";

    /** 00 */
    public static final String PATTERN_00 = "00";

    /**
     * md5加密
     * @param source
     * @return
     */
    public static String md5(String source) {  
        
        StringBuffer sb = new StringBuffer(32);  
              
        try {  
            MessageDigest md    = MessageDigest.getInstance("MD5");  
            byte[] array        = md.digest(source.getBytes("utf-8"));  
                  
            for (int i = 0; i < array.length; i++) {  
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));  
            }  
        } catch (Exception e) {  
            logger.error("Can not encode the string '" + source + "' to MD5!", e);  
            return null;  
        }  
              
        return sb.toString();  
    } 
    
    /**
     * <p>字符数字到数字转换</p>
     * @param strNumeric
     * @return 数字
     * @throws Exception 异常
     */
    public static BigDecimal getNumeric(String strNumeric) throws Exception {

        StringBuilder string = new StringBuilder();
        if (strNumeric.indexOf(",") >= 0) {
            String[] strArr = strNumeric.split(",");
            for (String str : strArr) {
                string.append(str);
            }
        } else {
            string.append(strNumeric);
        }
        try {
            return new BigDecimal(string.toString());
        } catch (NumberFormatException nfe) {
            throw new Exception(nfe);
        }
    }

    /**
     * <p>默认格式(#,##0.00)</p>
     * @param numeric 数字
     * @return 字符数字
     * @throws Exception 异常
     */
    public static String format(BigDecimal numeric) throws Exception {
        return format(numeric, PATTERN);
    }

    /**
     * <p>制定格式</p>
     * @param numeric 数字
     * @param pattern 格式
     * @return 字符数字
     * @throws Exception 异常
     */
    public static String format(BigDecimal numeric, String pattern) throws Exception {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(numeric.doubleValue());
    }

    /**
     * <p>制定格式</p>
     * @param numeric 数字
     * @param pattern 格式
     * @return 字符数字
     * @throws Exception 异常
     */
    public static String format(long numeric, String pattern) throws Exception {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(numeric);
    }
    
    /**
     * 创建文件夹
     * @param fileDir  文件夹创建的路径
     */
    public static void mkDir(String fileDir) {
        try {
            java.io.File file = new java.io.File(fileDir);
            if (!file.exists()) {
                if (!file.mkdirs()) {
                    throw new Exception("创建文件夹失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 保存图片
     * @throws IOException 
     */
    public static void savaIma(byte[] buffer,String url ) throws IOException{
//        mkDir(url);
    	mkDir(url.substring(0,url.lastIndexOf("\\")));
        OutputStream out=new FileOutputStream(url); 
        out.write(buffer, 0, buffer.length); 
        out.flush(); 
    }
    
    /**
     * 读取图片文件
     */
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
        byte[] bytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
        is.close();
        return bytes;
    }
    
    /**
     * 获取文件大小
     * @param f
     * @return
     * @throws Exception
     */
    public static long getFileSizes(File f) throws Exception{//取得文件大小
        long s=0;
        if (f.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(f);
           s= fis.available();
        } else {
            f.createNewFile();
            System.out.println("文件不存在");
        }
        return s;
    }
}
